﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Invisibility : Buff
    {
        public Spell_Invisibility() 
        {
            minutesLeft = 10;
        }

        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockitem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
                MapItem CurrentMapItem = null;
                List<Character> CharactersWithin4 = new List<Character>();
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                                CharactersWithin4.Add((Character)creaturebase);
                        }
                    }
                    if (mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                        CurrentMapItem = mapitem;
                }

                foreach (Character character in CharactersWithin4)
                {
                    character.AddMapItem(CurrentMapItem);
                }
            }
            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            TargetCreatureBase.buffManager.UpdateBuffs();

            MapItem CurrentMapItem = null;
            List<Character> CharactersWithin4 = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                            CharactersWithin4.Add((Character)creaturebase);
                    }
                }
                if (mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                    CurrentMapItem = mapitem;
            }

            foreach (Character character in CharactersWithin4)
            {
                character.AddMapItem(CurrentMapItem);
            }
        }
        public override void RemoveSpell(bool updateBuffs, bool sendMessage)
        {
            base.RemoveSpell(updateBuffs, sendMessage);

            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockitem.LockObj)
            {
                Map CurrentMap = TargetCreatureBase.GameHandler.GetMap(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
                MapItem CurrentMapItem = null;
                List<Character> CharactersWithin4 = new List<Character>();
                foreach (MapItem mapitem in CurrentMap.MapItems)
                {
                    if (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                        {
                            if (creaturebase is Character)
                                CharactersWithin4.Add((Character)creaturebase);
                        }
                    }
                    if (mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                        CurrentMapItem = mapitem;
                }

                foreach (Character character in CharactersWithin4)
                {
                    character.AddMapItem(CurrentMapItem);
                }
            }
        }

    }
}
